<template>
  <div>
    <el-container>
      <el-header class="homeHead">
        <div class="title">
          <img
            width="140px"
            height="64"
            src="~@/assets/images/logo.png"
            alt=""
          />
          <el-dropdown class="adminInfo" @command="commandHandler">
            <span class="el-dropdown-link">
              {{ adminInfo.name }}<i><img :src="adminInfo.avatar" /></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="adminInfo">个人中心</el-dropdown-item>
              <el-dropdown-item command="logout">注销登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <!-- select菜单激活回调 会寻找对应的index-->
          <!-- <el-menu @select="menuClick"> -->
          <div
            v-for="(item, index) in this.$router.options.routes"
            :key="index"
          >
            <el-menu v-if="!item.hidden" router>
              <el-submenu index="1">
                <template slot="title"
                  ><i class="el-icon-user-solid"></i
                  ><span style="margin-left: 5px">{{
                    item.name
                  }}</span></template
                >
                <el-menu-item
                  :index="item2.path"
                  v-for="(item2, indexj) in item.children"
                  :key="indexj"
                  ><span>{{ item2.name }}</span></el-menu-item
                >
              </el-submenu>
            </el-menu>
          </div>
        </el-aside>
        <el-main>
          <el-breadcrumb separator-class="el-icon-arrow-right" v-if="this.$router.currentRoute.path!='/home'" >
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{this.$router.currentRoute.name}}</el-breadcrumb-item>
          </el-breadcrumb>
          <div v-if="this.$router.currentRoute.path=='/home'">
            <div style="font-size: 30px; font-weight: bold;text-align:center"><span style="font-size:25px;margin-right:6px">{{adminInfo.name}}</span>,欢迎登录</div>
          </div>
          <!-- 显示路由的内容 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import cookie from "js-cookie";
let Base64 = require("js-base64").Base64;
export default {
  name: "Home",
  flag: false,
  data() {
    return {
      //token 分为3段 头为加密算法 尾部为自签名 中间为数据部分data
      token: cookie.get("token").split(".")[1],
      adminInfo: {
        name: "",
        id: "",
        avatar: "",
        exp: "",
      },
    };
  },
  methods: {
    commandHandler(command) {
      if (command == "logout") {
        this.$confirm("此操作将注销登录, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            cookie.set("token", "");
            this.$router.replace({ path: "/" });
            this.$message({
              type: "success",
              message: "注销成功!",
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消操作",
            });
          });
      }
      if (command == "adminInfo") {
        this.$router.push({ path: "/adminInfo" });
      }
    },
  },
  created() {
    this.adminInfo = JSON.parse(Base64.decode(this.token));
    // console.log(this.adminInfo);
  },
  mounted() {
    if (cookie.get("token")) {
      // console.log('aaa');
      return;
    } else {
      this.$router.replace("/");
    }
  },
};
</script>

<style scoped>
.homeHead {
  background-color: #ffffff;
  display: flex;
  align-items: center;
  padding: 0 15px;
  box-sizing: border-box;
  /* border-bottom: #409EFF 1px solid; */
}
.homeHead .title {
  font-size: 30px;
  font-family: 楷体;
  color: white;
  margin-top: 10px;
  margin-bottom: 10px;
}
.homeHead .adminInfo {
  cursor: pointer;
  left: 1050px;
}
.el-dropdown-link img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-left: 8px;
}
</style>
